﻿var userObj;

//用户管理页面上点击删除按钮弹出删除框(userlist.jsp)
function getcheckboxes(){
	var adminname = [];
	var values = [];
	var checkboxes = document.getElementsByName('adminIds');
	for (var i = 0; i < checkboxes.length; i++) {
		if (checkboxes[i].checked) {
			console.log(checkboxes[i].parentNode)
			console.log(checkboxes[i].checked)
			values.push(checkboxes[i].value);
			var tds=checkboxes[i].parentNode.parentNode;
			// console.log(tds)
			// console.log(tds.querySelectorAll('td'))
			// console.log(tds.querySelectorAll('td')[1].querySelector('span'));
			var span = tds.querySelectorAll('td')[1].querySelector('span')
			if (span){
				var spanValue = span.innerHTML; // 获取 span 元素的值
				adminname.push(spanValue);
				console.log(spanValue)
			}

		}
	}
	console.log(obj)
	console.log(values)
	console.log(adminname)
	return {values,adminname};
}
function deleteUser(obj){
	let result = getcheckboxes();
	console.log(result)
	console.log(result.values)
	console.log(result.adminname)
	let params;
	if (obj == undefined || obj.attr("adminid") == undefined){
		params = JSON.stringify(result.values);
	}else {
		params = JSON.stringify([obj.attr("adminid")]);
	}
	// return;
	$.ajax({
		type:"GET",
		url:path+"/gymjsp/admin.do",
		data:{method:"deluser",uid:params},
		dataType:"json",
		success:function(data){
			let content;
			if(data.delResult == "true"){//删除成功：移除删除行
				cancleBtn();
				if (obj == undefined){
					location.reload();
				}
				obj.parents("tr").remove();
			}else if(data.delResult == "false"){//删除失败
				//alert("对不起，删除用户【"+obj.attr("username")+"】失败");
				if (obj == undefined){
					content = "对不起，删除用户【"+result.adminname+"】失败";
					changeDLGContent(content);
				}else {
					changeDLGContent("对不起，删除用户【"+obj.attr("username")+"】失败");
				}

			}else if(data.delResult == "notexist"){
				//alert("对不起，用户【"+obj.attr("username")+"】不存在");
				if (obj == undefined ){
					content = "对不起，用户【"+result.adminname+"】不存在"
					changeDLGContent(content)
				}else {
					changeDLGContent("对不起，用户【"+obj.attr("username")+"】不存在");
				}
			}
		},
		error:function(data){
			//alert("对不起，删除失败");
			changeDLGContent("对不起，删除失败");

		}
	});
}

function openYesOrNoDLG(){
	$('.zhezhao').css('display', 'block');
	$('#removeUse').fadeIn();
}

function cancleBtn(){
	$('.zhezhao').css('display', 'none');
	$('#removeUse').fadeOut();
}
function changeDLGContent(contentStr){
	var p = $(".removeMain").find("p");
	p.html(contentStr);
}

$(function(){
	//通过jquery的class选择器（数组）
	//对每个class为viewUser的元素进行动作绑定（click）
	/**
	 * bind、live、delegate
	 * on
	 */
	$(".viewUser").on("click",function(){
		//将被绑定的元素（a）转换成jquery对象，可以使用jquery方法
		var obj = $(this);
		window.location.href=path+"/gymjsp/admin.do?method=view&aid="+ obj.attr("adminid");
	});
	
	$(".modifyUser").on("click",function(){
		var obj = $(this);
		window.location.href=path+"/gymjsp/admin.do?method=modify&aid="+ obj.attr("adminid");
	});

	$('#no').click(function () {
		cancleBtn();
	});
	
	$('#yes').click(function () {
		deleteUser(userObj);
	});

	$(".deleteUser").on("click",function(){
		console.log($(this))
		userObj = $(this);
		const result = getcheckboxes();
		let content;
		if (userObj.attr("username") === undefined){
			content = "你确定要删除用户【"+result.adminname+"】吗？"
		}else{
			content = "你确定要删除用户【"+userObj.attr("username")+"】吗？"
		}
		console.log(content)
		changeDLGContent(content)
		openYesOrNoDLG();
	});
	/*$(".deleteUser").on("click",function(){
		var obj = $(this);
		if(confirm("你确定要删除用户【"+obj.attr("username")+"】吗？")){
			$.ajax({
				type:"GET",
				url:path+"/jsp/user.do",
				data:{method:"deluser",uid:obj.attr("userid")},
				dataType:"json",
				success:function(data){
					if(data.delResult == "true"){//删除成功：移除删除行
						alert("删除成功");
						obj.parents("tr").remove();
					}else if(data.delResult == "false"){//删除失败
						alert("对不起，删除用户【"+obj.attr("username")+"】失败");
					}else if(data.delResult == "notexist"){
						alert("对不起，用户【"+obj.attr("username")+"】不存在");
					}
				},
				error:function(data){
					alert("对不起，删除失败");
				}
			});
		}
	});*/
});